package com.xpj.simulate;

import com.github.benmanes.caffeine.cache.LoadingCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ProductController {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    @GetMapping("/product/{productId}")
    public Product find(@PathVariable String productId){
        return productService.findProduct(productId);
    }

    @PostMapping("/product")
    public String update(@RequestBody Product product){
        productService.update(product);
        return "success";
    }

    @Autowired
    private ProductPreloadService productPreloadService;

    @PostMapping("/product/preload/{productId}")
    public String preload(@PathVariable String productId){
        productPreloadService.preloadSingle(productId);
        return "success";
    }

    @Autowired
    private LoadingCache loadingCache;
    @PostMapping("/cache/clear")
    public String clearLocalCache(){
        loadingCache.invalidateAll();
        return "success";
    }
}
